﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HealthAnalyzerRules.Delegation.Helpers
{

    public static class SMO_2008R2
    {
        public static string GetServiceAccount_SqlDB(string ServerName)
        {
            //var SqlConnInfo = new Microsoft.SqlServer.Management.Common.SqlConnectionInfo(
            //var SqlConn = new System.Data.SqlClient.SqlConnectionStringBuilder();
            //var SqlConn = new Microsoft.SqlServer.Management.Common.ServerConnection(
            return new Microsoft.SqlServer.Management.Smo.Server(ServerName).ServiceAccount;
        }
        public static string GetServiceAccount_SqlAgent(string ServerName)
        {
            return new Microsoft.SqlServer.Management.Smo.Server(ServerName).JobServer.ServiceAccount;
        }
        public static string GetServiceAccount_SqlRS(string ServerName)
        {
            throw new NotImplementedException();
            //return new Microsoft.SqlServer.Management.Smo.
        }
    }
    /// <summary>
    /// Analysis Management Objects
    /// </summary>
    /// <remarks>
    /// like SMO, for SQL AS
    /// </remarks>
    public static class AMO_2008R2
    {
        public static string GetServiceAccount_SqlDB(string ServerName)
        {
            //var SqlConnInfo = new Microsoft.SqlServer.Management.Common.SqlConnectionInfo(
            //var SqlConn = new System.Data.SqlClient.SqlConnectionStringBuilder();
            //var SqlConn = new Microsoft.SqlServer.Management.Common.ServerConnection(
            var Server = new Microsoft.AnalysisServices.Server();
            Server.Connect(ServerName);
            //Server.ServerProperties
            return "";
        }
    }

    public static class SQL
    {
        public static string GetSqlServiceAccount(System.Data.SqlClient.SqlConnection tConn)
        {
            string outval = String.Empty;

            if (tConn.State == System.Data.ConnectionState.Closed)
                tConn.Open();

            var tCmd = tConn.CreateCommand();
            tCmd.CommandType = System.Data.CommandType.Text;
            tCmd.CommandText = SqlCommand_GetSqlServiceAccount();
            outval = tCmd.ExecuteScalar() as string;
            tConn.Close();

            return outval;
        }
        public static string GetSqlAgentAccount(System.Data.SqlClient.SqlConnection tConn)
        {
            string outval = String.Empty;

            if (tConn.State == System.Data.ConnectionState.Closed)
                tConn.Open();

            var tCmd = tConn.CreateCommand();
            tCmd.CommandType = System.Data.CommandType.Text;
            tCmd.CommandText = SqlCommand_GetSqlServiceAccount();
            outval = tCmd.ExecuteScalar() as string;
            tConn.Close();

            return outval;
        }

        public static string SqlCommand_GetSqlServiceAccount()
        {
            //ref: http://dbamohsin.wordpress.com/2011/02/16/retrieve-the-sql-service-accounts-using-t-sql/
            return @"
DECLARE @NamedInstance bit
IF CAST(SERVERPROPERTY('ServerName') AS varchar) LIKE '%\%' SET @NamedInstance = 1 ELSE SET @NamedInstance = 0

DECLARE @ServiceName varchar(50)

IF @NamedInstance = 0
BEGIN
SET @ServiceName = 'MSSQLSERVER'
END
ELSE
BEGIN
SET @ServiceName = 'MSSQL$' + RIGHT(CAST(SERVERPROPERTY('ServerName') AS varchar),LEN(CAST(SERVERPROPERTY('ServerName') AS varchar)) - CHARINDEX('\',CAST(SERVERPROPERTY('ServerName') AS varchar),1))
END

DECLARE @KEY_VALUE varchar(100)
DECLARE @ServiceAccountName varchar(100) 

SET @KEY_VALUE = 'SYSTEM\CurrentControlSet\Services\' + @ServiceName
EXECUTE master..xp_regread 'HKEY_LOCAL_MACHINE', @KEY_VALUE, 'ObjectName', @ServiceAccountName OUTPUT
SELECT @ServiceAccountName as 'SQLService Account'
";
        }
        public static string SqlCommand_GetSqlAgentAccount()
        {
            //ref: http://dbamohsin.wordpress.com/2011/02/16/retrieve-the-sql-service-accounts-using-t-sql/
            return @"
DECLARE @NamedInstance bit
IF CAST(SERVERPROPERTY('ServerName') AS varchar) LIKE '%\%' SET @NamedInstance = 1 ELSE SET @NamedInstance = 0

IF @NamedInstance = 0
BEGIN
SET @ServiceName = 'SQLSERVERAGENT'
END
ELSE
BEGIN
SET @ServiceName = 'SQLAgent$' + RIGHT(CAST(SERVERPROPERTY('ServerName') AS varchar),LEN(CAST(SERVERPROPERTY('ServerName') AS varchar)) - CHARINDEX('\',CAST(SERVERPROPERTY('ServerName') AS varchar),1))
END

SET @KEY_VALUE = 'SYSTEM\CurrentControlSet\Services\' + @ServiceName

EXECUTE master..xp_regread 'HKEY_LOCAL_MACHINE', @KEY_VALUE, 'ObjectName', @ServiceAccountName OUTPUT
SELECT @ServiceAccountName as 'SQLAgent Account' 
";
        }
        public static string SqlCommand_GetSqlOlapAccount()
        {
            //ref: http://dbamohsin.wordpress.com/2011/02/16/retrieve-the-sql-service-accounts-using-t-sql/
            return @"
DECLARE @NamedInstance bit
IF CAST(SERVERPROPERTY('ServerName') AS varchar) LIKE '%\%' SET @NamedInstance = 1 ELSE SET @NamedInstance = 0

IF @NamedInstance = 0
BEGIN
SET @ServiceName = 'SQLSERVERAGENT'
END
ELSE
BEGIN
SET @ServiceName = 'MSOLAP$$' + RIGHT(CAST(SERVERPROPERTY('ServerName') AS varchar),LEN(CAST(SERVERPROPERTY('ServerName') AS varchar)) - CHARINDEX('\',CAST(SERVERPROPERTY('ServerName') AS varchar),1))
END

SET @KEY_VALUE = 'SYSTEM\CurrentControlSet\Services\' + @ServiceName

EXECUTE master..xp_regread 'HKEY_LOCAL_MACHINE', @KEY_VALUE, 'ObjectName', @ServiceAccountName OUTPUT
SELECT @ServiceAccountName as 'SQLAgent Account' 
";
        }
        public static string SqlCommand_IsSqlClustered()
        {
            // ref: http://msdn.microsoft.com/en-us/library/ms174396.aspx
            return @"SELECT SERVERPROPERTY ('IsClustered')";
        }
        public static string SqlCommand_IsSqlUsingActiveClusterNode()
        {
            return @"";
        }
    } // static class SQL

    public static class SQL_RS
    {
        public static string GetServiceAccount(string ServerName, string InstanceName)
        {
            //var Svc = System.ServiceProcess.ServiceController.GetServices(ServerName)
            //                               .Where(s => s.DisplayName.Contains("SQL Server Reporting Services"))
            //                               .Where(s => s.DisplayName.Contains(String.IsNullOrEmpty(InstanceName) ? "MSSQLSERVER" : InstanceName))
            //                               .Single();
            //return Svc.MachineName;
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// SQL Analysis Services (OLAP)
    /// </summary>
    public static class SQL_AS
    {
        public static string GetSqlServiceAccount()
        {
            return "";
        }
    }

} // namespace
